package com.soft.controller;

import com.github.pagehelper.PageInfo;
import com.soft.dao.TUserDao;
import com.soft.entity.TAdmin;
import com.soft.entity.TOrder;
import com.soft.entity.TProduct;
import com.soft.entity.TUser;
import com.soft.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * (TAdmin)表控制层
 *
 * @author makejava
 * @since 2021-12-24 13:58:27
 */
@Controller
@RequestMapping("tAdmin")
public class TAdminController {
    /**
     * 服务对象
     */
    @Resource
    private TAdminService tAdminService;
    @Resource
    private TProductService tProductService;
    @Resource
    private TCartService tCartService;

    @Resource
    private TUserService tUserService;

    @Resource
    private TUserDao tUserDao;

    @Resource
    private TOrderService tOrderService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public TAdmin selectOne(Long id) {
        return this.tAdminService.queryById(id);
    }
    @RequestMapping("admins")
//    @ResponseBody
    public String getAllUser(int offset, int limit, Model model){
        PageInfo<TAdmin> tAdmins = tAdminService.findAll(offset, limit);
        model.addAttribute("adminList", tAdmins);
        return "admin/admin";
    }
    @RequestMapping("index")
    public String index(Model model){
        Float total = 0.0f;
        model.addAttribute("userNumber", tUserDao.findAll().size());
        List<TOrder> list = tOrderService.queryAllOrders();
        for (TOrder tOrder: list){
            total += tOrder.getTotalPrice();
        }
        model.addAttribute("totalPay", total);
        model.addAttribute("orderNumber", tOrderService.queryAllOrders().size());
        model.addAttribute("average_price",total/tOrderService.queryAllOrders().size());
        return "admin/index";
    }

    @RequestMapping("admin/profile")
    public String to_profile(){
        return "admin/profile";
    }

    @RequestMapping("update")
    @ResponseBody
    public String admin_update(String old_pwd, String new_pwd, HttpSession session){
        TAdmin tAdmin = new TAdmin();
        tAdmin.setId(1L);//session
        tAdmin.setPassword(new_pwd);
        tAdminService.update(tAdmin);
        return "ok";
    }




    @RequestMapping("admin/login")
    public String adminlogin(String admin_name, String password, Model model, HttpSession session){
        TAdmin tAdmin = new TAdmin();
        TAdmin isExits =  tAdminService.queryByName(admin_name);
        if (isExits != null){
            tAdmin = tAdminService.queryByName(admin_name);
            //将用户信息加入session
            session.setAttribute("user", tAdmin);
            if(!tAdmin.getPassword().equals(password)){
                System.out.println("error!!!!!!");
                model.addAttribute("msg", "密码错误！");
                return "redirect:admin/login";
            }else{
                System.out.println("success!!!!!!");
                return "redirect:/admin";
            }
        }else{
            model.addAttribute("msg", "用户不存在！");
            System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11");
            return "redirect:admin/login";
        }
    }

    @RequestMapping("Calendar")
    public String toCalendar(){
        return "/admin/calendar";
    }

    @RequestMapping("support")
    public String toSupport(){
        return "/admin/support";
    }

    @RequestMapping("documentation")
    public String toDucumentation(){
        return "/admin/documentation";
    }

    @RequestMapping("searchProduct")
    public String searchProduct(Integer offset, Integer limit, String search, Model model){
        PageInfo<TProduct> pageInfo = tProductService.vagueFindAllProducts(offset, limit, search);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/all-product";
    }
}